package model

import (
	"gorm.io/datatypes"
	"time"
	"zsc-boot/app/system_layer/model"
	"zsc-boot/core/orm"
)

type CustomerTime int64

func (c CustomerTime) MarshalJSON() ([]byte, error) {
	return []byte("\"" + time.Unix(int64(c), 0).Format(time.DateTime) + "\""), nil
}

func (c CustomerTime) UnmarshalJSON(data []byte) error {
	dateTime, _ := time.Parse(time.DateTime, string(data))
	c = CustomerTime(dateTime.Unix())
	return nil
}

type Customer struct {
	orm.Model
	City           string                  `json:"city" gorm:"column:city;size:32"`
	UpdateUid      uint                    `json:"updateUid" gorm:"column:update_uid;comment:更新时间"` // 更新时间
	Name           string                  `json:"name" gorm:"column:name;size:128"`
	CompanyName    string                  `json:"companyName" gorm:"column:company_name;comment:公司名"`
	Category       string                  `json:"category" gorm:"size:8;column:category;comment:类型:ABC;default:B"`
	RegisterAmount uint                    `json:"registerAmount" gorm:"column:register_amount;comment:注册金额"`
	RealAmount     string                  `json:"realAmount" gorm:"column:real_amount;comment:实缴金额"`
	Mobile         string                  `json:"mobile" gorm:"column:mobile;index:inx_mobile;size:32"`
	Remark         string                  `json:"remark" gorm:"column:remark;type:text" `
	Notes          string                  `json:"notes" gorm:"column:notes;default:'';comment:点评"`
	Sex            string                  `json:"sex" gorm:"column:sex;default:'';size:8"`
	Age            uint8                   `json:"age" gorm:"column:age"`
	FollowStatus   string                  `json:"followStatus" gorm:"column:follow_status;default:'新线索';comment:跟进状态，新线索，待跟进，空号，停机，有意向，无意向，已加微信，已签约，转商机;size:32"`
	UserId         uint                    `json:"userId" gorm:"column:user_id;comment:归属员工id;index:inx_user_id;default:0"`
	UserInfo       model.ServiceUserSimple `json:"userInfo" gorm:"foreignKey:UserId"`
	ChannelId      uint                    `json:"channelId" gorm:"column:channel_id;comment:渠道id"`
	ChannelInfo    *SimpleCustomerChannel  `json:"channelInfo" gorm:"foreignKey:ChannelId"`
	LastFollowTime CustomerTime            `json:"lastFollowTime" gorm:"column:last_follow_time;comment:最后跟进时间;default:0;"`
	FollowLogs     []CustomerFollowLog     `json:"followLogs" gorm:"foreignKey:CustomerId"`
	FlowLogs       []CustomerFlowLog       `json:"flowLogs" gorm:"foreignKey:CustomerId"`
	AssignedTime   CustomerTime            `json:"assignedTime" gorm:"column:assigned_time;comment:分配时间;default:0"`
	ImportTime     CustomerTime            `json:"importTime" gorm:"column:import_time;comment:导入时间;default:0"`
	IsBlack        bool                    `json:"isBlack" gorm:"column:is_black;comment:是否黑名单用户;default:false"`
	IsCollect      bool                    `json:"isCollect" gorm:"column:is_collect;default:false;comment:是否收藏"`
	Tag            datatypes.JSON          `json:"tag" gorm:"column:tag;comment:客户标签"`
	IsImport       bool                    `json:"isImport" gorm:"column:is_import;comment:是否为导入数据;default:false"`
	IsNew          bool                    `json:"isNew" gorm:"column:is_new;comment:是否为新数据;default:true"`
	FirstUserId    uint                    `json:"firstUserId" gorm:"column:first_user_id;comment:初始用户ID"`
	Level          uint8                   `json:"level" gorm:"column:level;comment:星级;default:3"`
	NotFollowDay   int64                   `json:"notFollowDay" gorm:"-"`
}

func (Customer) TableName() string {
	return "customer"
}

func (Customer) TableComment() string {
	return "客户表"
}

type CustomerSimple struct {
	ID     uint   `json:"Id" form:"Id" gorm:"column:id;primaryKey;comment:主键ID"` // 主键ID
	Name   string `json:"name" gorm:"column:name;size:128"`
	Mobile string `json:"mobile" gorm:"column:mobile;index:inx_mobile;size:32"`
}

func (CustomerSimple) TableName() string {
	return "customer"
}

type UserCustomerCount struct {
	UserId uint `json:"id" gorm:"column:id;comment:用户id"`
	Count  uint `json:"customer_len" gorm:"column:customer_len;comment:客户数量"`
}

func (UserCustomerCount) TableName() string {
	return "service_user"
}
